package com.dingding.dao.usermangerdao.impl;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.dingding.bean.databean.request.ReqUserpushSetting;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.bean.entity.Userpushsetting;
import com.dingding.common.core.config.SystemCode;
import com.dingding.dao.basedao.impl.BaseDaoImpl;
import com.dingding.dao.usermangerdao.IUserpushSettingDao;

/**
 * @Title:UserpushSettingDaoImpl
 * @description:推送设置Dao
 * @author yi
 * 
 */
public class UserpushSettingDaoImpl extends BaseDaoImpl<Userpushsetting> implements IUserpushSettingDao {

	/**
	 * @Title:updateUserpushSetting
	 * @description:更新推送设置
	 * @param reqUserpushSetting
	 *            路线设置请求bean
	 * @param dataResponse
	 *            响应
	 * @author yi
	 */
	public void updateuserpushSetting(ReqUserpushSetting reqUserpushSetting, DataResponse dataResponse) {

		Userpushsetting setting = null;
		// 根据用户Id创建查询条件
		Criterion criterion = Restrictions.eq("loginId", reqUserpushSetting.getUserId());
		try {
			// 获取用户的路线推送设置信息
			setting = this.queryCriteriaUnResult(criterion, false);
		} catch (Exception e) {
			log.info("获取用户推送设置失败", e);
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		// 设置新订单提醒
		setting.setOrderReminder(reqUserpushSetting.getOrderReminder());
		// 设置周末提醒
		setting.setWeekendPush(reqUserpushSetting.getWeekendPush());
		// 设置顺路程度
		setting.setRegularRouteConditions(reqUserpushSetting.getRegularRouteConditions());
		//保存
		try {
			this.save(setting);
		} catch (Exception e) {
			log.info("用户路线推送存储失败",e);
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}
		
		


	}

	/**
	 * 
	 * @Title: setStatus @Description:根据状态码设置响应信息 @param dataResponse
	 *         响应信息封装bean @param status 状态码 @author Li Yi @return void @throws
	 */
	public void setStatus(DataResponse dataResponse, int status) {
		String message = null;
		switch (status) {
		case SystemCode.SUCCESS:// 请求成功
			message = SystemCode.SUCCESS_MESSAGE;
			break;
		case SystemCode.VALIDATE_THROUGH_ERROR:// 手机验证码失效
			message = SystemCode.VALIDATE_THROUGH_MESSAGE;
			break;
		case SystemCode.ERROR:// 系统异常
			message = SystemCode.ERROR_MESSAGE;
			break;
		case SystemCode.DATA_NULL_ERROR:// 客户端请求数据为空
			message = SystemCode.DATA_NULL_MESSAGE;
			break;
		case SystemCode.DATA_NULL_PARAM:// 客户端请求参数为空
			message = SystemCode.DATA_NULL_PARAM_MESSAGE;
			break;
		case SystemCode.REGISTER_USER_EXIST_ERROR:// 用户已存在
			message = SystemCode.REGISTER_USER_EXIST_MESSAGE;
			break;
		case SystemCode.USER_INFO_NOT_EXIST_ERROR:// 用户不存在
			message = SystemCode.USER_INFO_NOT_EXIST_MESSAGE;
			break;
		case SystemCode.PHONE_NUM_NOT_MATCH_ERROR:// 手机格式不正确
			message = SystemCode.PHONE_NUM_NOT_MATCH_MESSAGE;
			break;
		case SystemCode.USER_ROUTE_NOT_EXIST:// 用户路线不存在
			message = SystemCode.USER_ROUTE_NOT_EXIST_MESSAGE;
		case SystemCode.ADDRESS_NOT_SET:// 地址未设置
			message = SystemCode.ADDRESS_NOT_SET_MESSAGE;
			break;
		case SystemCode.ADD_USER_ROUTE:// 用户路线不能大于四条
			message = SystemCode.ADD_USER_ROUTE_MESSAGE;
			break;
		}

		dataResponse.setResponseCode(status);
		dataResponse.setResponseMessage(message);
	}

}
